<?php

namespace App\Http\Controllers\V1\API;

use App\Http\Controllers\V1\API\BaseController;
use App\Models\V1\ExcellentTeachers;
use App\Models\V1\Faculty;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Auth;
use Log;

class ExcellentTeachersController extends BaseController
{
    /****
     * 宿舍申请接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function teachersData(Request $request) {
        $result = array(
            'errorCode' => Response::HTTP_NOT_FOUND,
            'errorMsg' => Response::$statusTexts[Response::HTTP_NOT_FOUND],
            'data' => array()
        );

        $teacherList = $this->getTeachersList();
        if ($teacherList->isEmpty()) {
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = Response::$statusTexts[Response::HTTP_NO_CONTENT];
        }else {
            // 格式化数据
            $teachers = array();
            foreach ($teacherList as $item) {
                $formatTeacher = array();
                $formatTeacher['teacherCode'] = $item['code'];
                $formatTeacher['avatar'] = $item['avatar'];
                $teacher = $this->getFacultyByCode($item['code']);
                if (!empty($teacher)) {
                    $formatTeacher['name'] = $teacher['name'];
                    $formatTeacher['title'] = isset($teacher['title'])?$teacher['title']:'';
                    $formatTeacher['specialty'] = isset($teacher['specialty'])?$teacher['specialty']:'';
                    $formatTeacher['department'] = isset($teacher['department_name'])?$teacher['department_name']:'';
                }else {
                    $formatTeacher['name'] = '';
                    $formatTeacher['title'] = '';
                    $formatTeacher['specialty'] = '';
                    $formatTeacher['department'] = '';
                }
                $content = json_decode($item['content'], true);
                $formatTeacher['content'] = $content;
                array_push($teachers, $formatTeacher);
            }
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = Response::$statusTexts[Response::HTTP_OK];
            $result['data'] = $teachers;
        }
        return $this->sendResponse($result, $result['errorCode']);
    }

    /****
     * 获取所有名师
     * @return \Illuminate\Support\Collection
     */
    private function getTeachersList()
    {
        $data = ExcellentTeachers::orderBy('id','asc')->get();
        return $data;
    }

    /**
     * 根据教职工代码获取教职工信息
     *
     * @param $code
     * @return \Illuminate\Database\Eloquent\Model|null|static
     */
    private function getFacultyByCode($code)
    {
        $faculty = Faculty::where('code', $code)->first();
        if (!empty($faculty)) {
            $objDepartment = $faculty->department;
            $faculty['department_name'] = $objDepartment['name'];
        }
        return $faculty;
    }

    /***
     * 验证参数
     * @param Request $request
     * @return mixed
     */
    public function validateTeacher(Request $request)
    {
        //校验规则
        $rules = array();
        $rules['userID'] = 'required';
        $rules['userID'] = 'required|exists:basic_new_students,exam_code,deleted_at,NULL|bail';

        //错误提示信息
        $messages = array();
        $messages['userID.required'] = '用户代码不能为空';
        $messages['userID.exists'] = '用户不存在';
        $validator = Validator::make($request->all(),$rules,$messages);  //第一个参数是提交的数据，第二个是校对规则，第三个是错误信息
        $errors = $validator->errors()->all(); //这是捕获到的所有参数验证的错误信息
        if(!empty($errors)) {
            $first_errors = $errors[0];
            $result['errorCode'] = Response::HTTP_BAD_REQUEST;
            $result['errorMsg'] = $first_errors;
            return $result;
        }
    }
}
